package com.ruyue.csinstall.command.impl;

import com.ruyue.csinstall.command.Command;
import com.ruyue.csinstall.model.CsPro;
import com.ruyue.csinstall.utils.Utils;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.util.Collection;

/**
 * 解压升级包
 * @author xiawl
 * @create 2017/7/23 14:35.
 */
public class UnZipCommand implements Command {
    private static final Log log = LogFactory.getLog(UnZipCommand.class);
    private String zipPath;
    private String unZipPath;

    public UnZipCommand(CsPro csPro) {
        this.zipPath = csPro.getPackagePath();
        this.unZipPath = csPro.getUnZipPath();
    }

    @Override
    public boolean execute() {
        log.info(String.format("开始扫描[%s]存在的\"comm-eccg-server.*.zip\"命名规则的升级包(若有多个请确保它们隶属同一版本)", zipPath));
        Collection<File> listFiles = FileUtils.listFiles(new File(zipPath), new RegexFileFilter("^comm-eccg-server.*.zip$"), null);
        log.info(String.format("扫描结果..."));
        int count = 0;
        for(File f : listFiles){
            Utils.sleep(300);
            count++;
            log.info(String.format("%s. %s", count, f.getName()));
        }

        for(File f : listFiles){
            Utils.sleep(100);
            if(!unZip(f.getAbsolutePath())){
                return false;
            }
        }
        return true;
    }

    private boolean unZip(String zip) {
        ArchiveInputStream in = null;
        try {
            log.info(String.format("解压升级包[%s]->[%s]", zip, unZipPath));
            File warFile = new File(zip);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(warFile));
            in = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.JAR, bufferedInputStream);
            JarArchiveEntry entry;
            while ((entry = (JarArchiveEntry) in.getNextEntry()) != null) {
                boolean exist = new File(String.format("%s/%s", unZipPath, entry.getName())).exists();
                if (entry.isDirectory()) {
                    new File(unZipPath, entry.getName()).mkdir();
                } else {
                    if(!exist){//多个APP时,只覆盖apps目录下文件即可
                        OutputStream out = FileUtils.openOutputStream(new File(unZipPath, entry.getName()));
                        IOUtils.copy(in, out);
                        out.close();
                    }
                }
            }
            return true;
        } catch (Exception e) {
            log.error("解压升级包出错", e);
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
        return false;
    }

}